package com.jiankangjin.alipaychannel.alipay.config;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class AlipayConfig {
	private static final transient Log logger = LogFactory.getLog(AlipayConfig.class);
	// 合作身份者ID
	public static String PARTNER;
	// 商户的私钥
	public static String PRIVATE_KEY;

	// 支付宝的公钥，无需修改该值
	public static String ALI_PUBLIC_KEY  = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCnxj/9qwVfgoUh/y2W89L6BkRAFljhNhgPdyPuBV64bfQNN1PjbCzkIM6qRdKBoLPXmKKMiFYnkd6rAoprih3/PrQEB/VsW8OoM8fxn67UDYuyBTqA23MML9q1+ilIZwBC2AQ2UBVOrFXfFl75p6/B5KsiNG9zpgmLCUYuLkxpLQIDAQAB";

	public static String NOTIFY_URL;
	
	public static String EMAIL;

	// 字符编码格式 目前支持 gbk 或 utf-8
	public static String INPUT_CHARSET = "utf-8";
	
	// 签名方式 不需修改
	public static String SIGN_TYPE = "RSA";
	
	static {
		AlipayConfig.loade();
	}
	
	/**
	 * loade 加载AlipayConfig.properties文件
	 */
	private static void loade() {
		try {
			InputStream inputStream = AlipayConfig.class.getClassLoader().getResourceAsStream("AlipayConfig.properties");
			Properties pops = new Properties();
			pops.load(inputStream);
			// 返回Properties中包含的key-value的Set视图
			Set<Entry<Object, Object>> set = pops.entrySet();
			// 返回在此Set中的元素上进行迭代的迭代器
			Iterator<Map.Entry<Object, Object>> it = set.iterator();
			String key = null;
			String value = null;
			// 循环取出key-value
			Map<String, String> propertiesMap = new HashMap<String, String>();
			while (it.hasNext()) {
				Entry<Object, Object> entry = it.next();
				key = String.valueOf(entry.getKey());
				value = String.valueOf(entry.getValue());
				key = key == null ? key : key.trim();
				value = value == null ? value : value.trim();
				// 将key-value放入map中
				propertiesMap.put(key, value);
			}
			if (pops != null) {
				PARTNER = propertiesMap.get("partner");
				PRIVATE_KEY = propertiesMap.get("private_key");
				EMAIL = propertiesMap.get("email");
				NOTIFY_URL = propertiesMap.get("notify_url");
			}
		} catch (Exception e) {
			logger.error("Load file AlipayConfig.properties failed!!", e);
		}
	}
}
